<template>
   <div :class="y>=val+100?'goods-tabs-sticky':'goods-tabs'" ref="target">
    <div class="navs">
    <nav @click="gotop()">
      <a @click="activeName='GoodsDetail'" :class="{active:activeName==='GoodsDetail'}" href="javascript:;">商品详情</a>
      <a @click="activeName='GoodsComment'" :class="{active:activeName==='GoodsComment'}" href="javascript:;">商品评价<span>{{goods.commentCount}}</span></a>
    </nav>
    </div>
    <!-- 切换内容的地方 -->
    <!-- 动态切换组件用动态组件component -->
    <!-- keep-alive可以缓存动态切换组件前的页面 -->
    <div class="content">
    <!-- <keep-alive> -->
      <component :is="activeName"></component>
    <!-- </keep-alive> -->
    </div>
    <!-- <GoodsDetail v-if="activeName==='GoodsDetail'" />
    <GoodsComment v-if="activeName==='GoodsComment'" /> -->
  </div>
</template>

<script>
import { inject, onMounted, ref } from 'vue'
import { useWindowScroll } from '@vueuse/core'
import GoodsDetail from './goods-detail.vue'
import GoodsComment from './goods-comment.vue'
export default {
  name: 'GoodsTabs',
  components: { GoodsDetail, GoodsComment },
  setup () {
    // Goods组件传来的goods详情数据
    const goods = inject('goods')
    const activeName = ref('GoodsDetail')
    const target = ref(null)
    const { y } = useWindowScroll(target)
    const val = ref(0)
    onMounted(() => {
      console.log('target', target.value.offsetTop);
      val.value = target.value.offsetTop
    })
    // 回到tabs顶部
    const gotop = () => {
      // document.body.scrollTop = val.value + 100
      // document.documentElement.scrollTop = val.value + 100
      const timer = setInterval(() => {
        const top = document.documentElement.scrollTop || document.body.scrollTop;
        const speed = Math.floor(-top / 10);
        document.documentElement.scrollTop = document.body.scrollTop = top + speed;
        if (document.documentElement.scrollTop <= val.value + 100) {
          // 当小于tabs的scrollTop时，将body的scrollTop设为tabs的scrollTop
          document.documentElement.scrollTop = val.value + 100
          clearInterval(timer);
        }
      }, 15)
    }
    return { activeName, goods, target, y, val, gotop }
  }
}
</script>

<style lang='less' scoped>
.goods-tabs {
  min-height: 600px;
  background: #fff;
  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;
      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
      &:first-child {
        border-right: 1px solid #f5f5f5;
      }
      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @themeColor;
        }
      }
    }
  }
}
.goods-tabs-sticky {
  min-height: 600px;
  background: #fff;
  .navs {
    z-index: 1000;
    position: fixed;
    top: 80px;
    min-width: 940px;
    nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    background: #fff;
    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;
      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }
      &:first-child {
        border-right: 1px solid #f5f5f5;
      }
      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @themeColor;
        }
      }
    }
  }
  }
  .content {
    &::before {
      content: '';
      display: block;
      height: 70px;
    }
  }
}
</style>
